<?php
	require_once 'bin/medoo.min.php';
	require_once 'bin/functions.php';
	require_once 'DataScraper.php';
	require_once 'Database.php';
	require_once 'DataScraperToWordpress.php';	
	
	
	$mode = CONF::$modeType;
	
	
	//TO DEBUG A CERTAIN LINK, INSERT IT INTO $links ARRAY
	$links = array();
	
	if(count($links) == 0)
	{
		$datasets = explode("\n", file_get_contents("datasets.txt"));
		foreach($datasets as $link)
		{
			if(substr(trim($link), 0, 2) != "//" && trim($link) != "")
			{
				array_push($links, $link);
			}
		}
	}
	
	//IF UPDATING ALL LINKS:-
	if($mode == 2)
	{
		//WORDPRESS LINKS EXTRACTOR
		$links_pre = Database::Select(CONF::$WPPrefex."dataset", array("dataset_id", "url"), array(
			"wp_post_ID" => Database::Select(CONF::$WPPrefex."posts", "id", array("AND"=>array("post_status"=>"publish", "post_type"=>"dataset")))
		));
		var_dump($links_pre);
		/*
		foreach($links_pre as $_link)
		{
			$__link = $_link;
			addToArray($links, $__link, true);
		}
		*/
	}
	
	//array to store data retrieved
	$array_data_retrieved = DataScraper::executeDataScraping($links, $mode == 1);
	
	if($mode == 0)
	{
		var_dump($array_data_retrieved);
		die();
	}
	else
	{
		echo "<br>[Retrieved Data, Added/Updated Data, \$links var]:<br>";
		//log the insertion of the dataRetrieved into the database
		$logs = DataScraperToWordPress::inputDataIntoDatabase($array_data_retrieved);
		?>
		
		<pre><?= var_dump($logs) ?></pre>
		<pre><?= var_dump($links) ?></pre>
		<pre><?= var_dump($array_data_retrieved) ?></pre>
		<?php
	}
?>